﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace cellAnalysis
{
    class ModulParameters
    {
        private TextDatei textWriter;

        private bool m_CellSegmentation;
        private bool m_CoreDetection;
        private bool m_ClusterSplitting;
        private bool m_ShowCellCores;

        private byte m_Thres_R_LowVal;
        private byte m_Thres_R_UpVal;
        private byte m_Thres_G_LowVal;
        private byte m_Thres_G_UpVal;
        private byte m_Thres_B_LowVal;
        private byte m_Thres_B_UpVal;

        private int m_minCellArea;

        private byte m_Holes_Thres_R_LowVal;
        private byte m_Holes_Thres_R_UpVal;
        private byte m_Holes_Thres_G_LowVal;
        private byte m_Holes_Thres_G_UpVal;
        private byte m_Holes_Thres_B_LowVal;
        private byte m_Holes_Thres_B_UpVal;

        private bool m_FindCellHoles;
        private bool m_DynamicRefresh;

        private int m_Bins;
        private List<String> m_TemplatePaths = new List<String>();

        public  ModulParameters()
        {
            textWriter = new TextDatei();
        }
        public ModulParameters(String filename)
        {
            textWriter = new TextDatei();
            LoadParFile(filename);
        }

        public bool CellSegmentation
        {
            get
            {
                return m_CellSegmentation;
            }
            set
            {
                m_CellSegmentation = value;
            }
        }
        public bool CoreDetection
        {
            get
            {
                return m_CoreDetection;
            }
            set
            {
                m_CoreDetection = value;
            }
        }
        public bool ClusterSplitting
        {
            get
            {
                return m_ClusterSplitting;
            }
            set
            {
                m_ClusterSplitting = value;
            }
        }
        public bool ShowCellCores
        {
            get
            {
                return m_ShowCellCores;
            }
            set
            {
                m_ShowCellCores = value;
            }
        }

        public byte Thresh_R_LowVal
        {
            get
            {
                return m_Thres_R_LowVal;
            }
            set
            {
                m_Thres_R_LowVal = value;
            }
        }
        public byte Thresh_R_UpVal
        {
            get
            {
                return m_Thres_R_UpVal;
            }
            set
            {
                m_Thres_R_UpVal = value;
            }
        }
        public byte Thresh_G_LowVal
        {
            get
            {
                return m_Thres_G_LowVal;
            }
            set
            {
                m_Thres_G_LowVal = value;
            }
        }
        public byte Thresh_G_UpVal
        {
            get
            {
                return m_Thres_G_UpVal;
            }
            set
            {
                m_Thres_G_UpVal = value;
            }
        }
        public byte Thresh_B_LowVal
        {
            get
            {
                return m_Thres_B_LowVal;
            }
            set
            {
                m_Thres_B_LowVal = value;
            }
        }
        public byte Thresh_B_UpVal
        {
            get
            {
                return m_Thres_B_UpVal;
            }
            set
            {
                m_Thres_B_UpVal = value;
            }
        }

        public int MinCellArea
        {
            get
            {
                return m_minCellArea;
            }
            set
            {
                m_minCellArea = value;
            }
        }

        public byte Thresh_Holes_R_LowVal
        {
            get
            {
                return m_Holes_Thres_R_LowVal;
            }
            set
            {
                m_Holes_Thres_R_LowVal = value;
            }
        }
        public byte Thresh_Holes_R_UpVal
        {
            get
            {
                return m_Holes_Thres_R_UpVal;
            }
            set
            {
                m_Holes_Thres_R_UpVal = value;
            }
        }
        public byte Thresh_Holes_G_LowVal
        {
            get
            {
                return m_Holes_Thres_G_LowVal;
            }
            set
            {
                m_Holes_Thres_G_LowVal = value;
            }
        }
        public byte Thresh_Holes_G_UpVal
        {
            get
            {
                return m_Holes_Thres_G_UpVal;
            }
            set
            {
                m_Holes_Thres_G_UpVal = value;
            }
        }
        public byte Thresh_Holes_B_LowVal
        {
            get
            {
                return m_Holes_Thres_B_LowVal;
            }
            set
            {
                m_Holes_Thres_B_LowVal = value;
            }
        }
        public byte Thresh_Holes_B_UpVal
        {
            get
            {
                return m_Holes_Thres_B_UpVal;
            }
            set
            {
                m_Holes_Thres_B_UpVal = value;
            }
        }

        public bool FindCellHoles
        {
            get
            {
                return m_FindCellHoles;
            }
            set
            {
                m_FindCellHoles = value;
            }
        }
        public bool DynamicRefresh
        {
            get
            {
                return m_DynamicRefresh;
            }
            set
            {
                m_DynamicRefresh = value;
            }
        }

        public int Bins
        {
            get
            {
                return m_Bins;
            }
            set
            {
                m_Bins = value;
            }
        }
        public List<String> TemplatePaths
        {
            get
            {
                return m_TemplatePaths;
            }
            set
            {
                m_TemplatePaths = value;
            }
        }

        public void LoadParFile(String filename)
        {
            this.CellSegmentation = Convert.ToBoolean(textWriter.ReadLine(filename, 1));
            this.CoreDetection    = Convert.ToBoolean(textWriter.ReadLine(filename, 2));
            this.ClusterSplitting = Convert.ToBoolean(textWriter.ReadLine(filename, 3));
            this.ShowCellCores = Convert.ToBoolean(textWriter.ReadLine(filename, 4));

            this.Thresh_R_LowVal = Convert.ToByte(textWriter.ReadLine(filename, 5));
            this.Thresh_R_UpVal = Convert.ToByte(textWriter.ReadLine(filename, 6));
            this.Thresh_G_LowVal = Convert.ToByte(textWriter.ReadLine(filename, 7));
            this.Thresh_G_UpVal = Convert.ToByte(textWriter.ReadLine(filename, 8));
            this.Thresh_B_LowVal = Convert.ToByte(textWriter.ReadLine(filename, 9));
            this.Thresh_B_UpVal = Convert.ToByte(textWriter.ReadLine(filename, 10));

            this.MinCellArea = Convert.ToInt32(textWriter.ReadLine(filename, 11));

            this.Thresh_Holes_R_LowVal = Convert.ToByte(textWriter.ReadLine(filename, 12));
            this.Thresh_Holes_R_UpVal = Convert.ToByte(textWriter.ReadLine(filename, 13));
            this.Thresh_Holes_G_LowVal = Convert.ToByte(textWriter.ReadLine(filename, 14));
            this.Thresh_Holes_G_UpVal = Convert.ToByte(textWriter.ReadLine(filename, 15));
            this.Thresh_Holes_B_LowVal = Convert.ToByte(textWriter.ReadLine(filename, 16));
            this.Thresh_Holes_B_UpVal = Convert.ToByte(textWriter.ReadLine(filename, 17));

            this.FindCellHoles = Convert.ToBoolean(textWriter.ReadLine(filename, 18));
            this.DynamicRefresh = Convert.ToBoolean(textWriter.ReadLine(filename, 19));

            this.Bins = Convert.ToInt32(textWriter.ReadLine(filename, 20));
            
            m_TemplatePaths.Clear();
            int i=21;
            while (textWriter.ReadLine(filename, i) != "")
            {
                this.m_TemplatePaths.Add(textWriter.ReadLine(filename, i));
                i++;
            }
        }
        public void SaveParFile(String filename)
        {
            //Zuerst die Datei löschen falls vorhanden
            if (File.Exists(filename))
            {
                FileInfo fi = new FileInfo(filename);
                if (fi.Exists)
                    fi.Delete();
            }
            textWriter.WriteLine(filename, 1, this.CellSegmentation.ToString(),true);
            textWriter.WriteLine(filename, 2, this.CoreDetection.ToString(), true);
            textWriter.WriteLine(filename, 3, this.ClusterSplitting.ToString(), true);
            textWriter.WriteLine(filename, 4, this.ShowCellCores.ToString(), true);

            textWriter.WriteLine(filename, 5, this.Thresh_R_LowVal.ToString(), true);
            textWriter.WriteLine(filename, 6, this.Thresh_R_UpVal.ToString(), true);
            textWriter.WriteLine(filename, 7, this.Thresh_G_LowVal.ToString(), true);
            textWriter.WriteLine(filename, 8, this.Thresh_G_UpVal.ToString(), true);
            textWriter.WriteLine(filename, 9, this.Thresh_B_LowVal.ToString(), true);
            textWriter.WriteLine(filename, 10, this.Thresh_B_UpVal.ToString(), true);

            textWriter.WriteLine(filename, 11, this.MinCellArea.ToString(), true);

            textWriter.WriteLine(filename, 12, this.Thresh_Holes_R_LowVal.ToString(), true);
            textWriter.WriteLine(filename, 13, this.Thresh_Holes_R_UpVal.ToString(), true);
            textWriter.WriteLine(filename, 14, this.Thresh_Holes_G_LowVal.ToString(), true);
            textWriter.WriteLine(filename, 15, this.Thresh_Holes_G_UpVal.ToString(), true);
            textWriter.WriteLine(filename, 16, this.Thresh_Holes_B_LowVal.ToString(), true);
            textWriter.WriteLine(filename, 17, this.Thresh_Holes_B_UpVal.ToString(), true);

            textWriter.WriteLine(filename, 18, this.FindCellHoles.ToString(), true);
            textWriter.WriteLine(filename, 19, this.DynamicRefresh.ToString(), true);

            textWriter.WriteLine(filename, 20, this.Bins.ToString(), true);
            for (int i = 0; i < TemplatePaths.Count; i++)
            {
                textWriter.WriteLine(filename, 21+i, this.TemplatePaths[i], true);
            }
        }
    }
}


